package com.uidt.project.ptsj.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.uidt.common.utils.DateUtils;
import com.uidt.common.utils.SecurityUtils;
import com.uidt.common.utils.StringUtils;
import com.uidt.framework.security.LoginUser;
import com.uidt.project.ptsj.domain.PlaceInfo;
import com.uidt.project.ptsj.mapper.PlaceInfoMapper;
import com.uidt.project.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.uidt.project.ptsj.mapper.HouseInfoMapper;
import com.uidt.project.ptsj.domain.HouseInfo;
import com.uidt.project.ptsj.service.IHouseInfoService;

/**
 * 房源信息Service业务层处理
 * 
 * @author uidt
 * @date 2024-08-15
 */
@Service
public class HouseInfoServiceImpl implements IHouseInfoService 
{
    @Autowired
    private HouseInfoMapper ptsjHouseInfoMapper;

    @Autowired
    private PlaceInfoMapper placeInfoMapper;

    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询房源信息
     *
     * @param houseCode 房源信息主键
     * @return 房源信息
     */
    @Override
    public HouseInfo selectHouseInfoByHouseCode(String houseCode)
    {
        HouseInfo houseInfo = ptsjHouseInfoMapper.selectHouseInfoByHouseCode(houseCode);
        if(houseInfo!=null && StringUtils.isNotEmpty(houseInfo.getPlaceCode())){
            PlaceInfo placeInfo =placeInfoMapper.selectPlaceInfoByPlaceCode(houseInfo.getPlaceCode());
            houseInfo.setPlaceInfo(placeInfo);
        }
        return houseInfo;
    }

    /**
     * 查询房源信息列表
     *
     * @param ptsjHouseInfo 房源信息
     * @return 房源信息
     */
    @Override
    public List<HouseInfo> selectHouseInfoList(HouseInfo ptsjHouseInfo)
    {
        LoginUser user = SecurityUtils.getLoginUser();
        if(!user.getUser().isAdmin()){
            if(user.getUser().getDept()!=null && StringUtils.isNotEmpty(user.getUser().getDept().getDeptcode())){
                ptsjHouseInfo.setGxdwdm(sysDeptService.getDeptAuthByCodeForPlaceAndHouse(user.getUser().getDept().getDeptcode()));
            }else{
                return new ArrayList<HouseInfo>();
            }
        }
        return ptsjHouseInfoMapper.selectHouseInfoList(ptsjHouseInfo);
    }

    /**
     * 新增房源信息
     *
     * @param ptsjHouseInfo 房源信息
     * @return 结果
     */
    @Override
    public int insertHouseInfo(HouseInfo ptsjHouseInfo)
    {
        return ptsjHouseInfoMapper.insertHouseInfo(ptsjHouseInfo);
    }

    /**
     * 修改房源信息
     *
     * @param ptsjHouseInfo 房源信息
     * @return 结果
     */
    @Override
    public int updateHouseInfo(HouseInfo ptsjHouseInfo)
    {
        ptsjHouseInfo.setUpdateTime(DateUtils.getNowDate());
        return ptsjHouseInfoMapper.updateHouseInfo(ptsjHouseInfo);
    }

    /**
     * 批量删除房源信息
     *
     * @param houseCodes 需要删除的房源信息主键
     * @return 结果
     */
    @Override
    public int deleteHouseInfoByHouseCodes(String[] houseCodes)
    {
        return ptsjHouseInfoMapper.deleteHouseInfoByHouseCodes(houseCodes);
    }

    /**
     * 删除房源信息信息
     *
     * @param houseCode 房源信息主键
     * @return 结果
     */
    @Override
    public int deleteHouseInfoByHouseCode(String houseCode)
    {
        return ptsjHouseInfoMapper.deleteHouseInfoByHouseCode(houseCode);
    }
}
